<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ props: {disabled: disableSubmit} }"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="推送类别">
          <j-dict-select-tag v-decorator="['remark1', validatorRules.remark1]"
                             :triggerChange="true"
                             placeholder="请选择推送类别"
                             dictCode='tsrlb' @change="changeRem"/>
        </a-form-item>

        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="消息类别">
          <j-dict-select-tag v-decorator="['remark3', validatorRules.remark3]"
                             :triggerChange="true"
                             placeholder="请选择消息类别"
                             dictCode='xxlb'/>
        </a-form-item>-->

        <a-form-item
          v-if="flag"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="接收人">
            <j-select-games-user v-model="gamesUser" @change="getGamesUser"></j-select-games-user>

            <a-input hidden placeholder="请输入接收人" :read-only="disableSubmit" v-decorator="['userId', {}]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="消息标题">
          <a-input placeholder="请输入消息标题" :read-only="disableSubmit" v-decorator="['title', validatorRules.title]" :max-length="20"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="常用语类别">
          <j-dict-select-tag v-decorator="['remark2', {}]"
                             :triggerChange="true"
                             placeholder="请选择常用语类别"
                             dictCode='cyy' @change="getContent"/>
        </a-form-item>

        <a-form-item
          v-if="contentFlagYes"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="消息内容">
          <a-select placeholder="请选择消息内容"
                    v-decorator="['content',  validatorRules.content]">
                    <a-select-option v-for="d in contentList" :key="d.content" :value="d.content">{{ d.content }}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          v-if="contentFlagNo"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="消息内容">
          <a-textarea  placeholder="请输入消息内容" :read-only="disableSubmit" :autosize="{minRows: 5,maxRows: 10}" :max-length="100" v-decorator="['content',  validatorRules.content]" />
        </a-form-item>
        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="0:正常,1:删除">
          <a-input placeholder="请输入0:正常,1:删除" v-decorator="['delflag', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark">
          <a-input placeholder="请输入remark" v-decorator="['remark', {}]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark2">
          <a-input placeholder="请输入remark2" v-decorator="['remark2', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark3">
          <a-input placeholder="请输入remark3" v-decorator="['remark3', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark4">
          <a-input placeholder="请输入remark4" v-decorator="['remark4', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark5">
          <a-input placeholder="请输入remark5" v-decorator="['remark5', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark6">
          <a-input placeholder="请输入remark6" v-decorator="['remark6', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark7">
          <a-input placeholder="请输入remark7" v-decorator="['remark7', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark8">
          <a-input placeholder="请输入remark8" v-decorator="['remark8', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark9">
          <a-input placeholder="请输入remark9" v-decorator="['remark9', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark10">
          <a-input placeholder="请输入remark10" v-decorator="['remark10', {}]" />
        </a-form-item>-->

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import JSelectGamesUser from '@/components/jeecgbiz/JSelectGamesUser'
  import { getAction } from '@/api/manage'

  export default {
    name: "GMessageModal",
    components: {
      JSelectGamesUser
    },
    data () {

      return {
        flag:false,
        contentFlagNo:true,
        contentFlagYes:false,
        contentList:[],
        gamesUser:'',
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          remark1:{rules: [{ required: true, message: '请选择推送类别' }]},
          remark3:{rules: [{ required: true, message: '请选择消息类别' }]},
          content:{rules: [{ required: true, message: '请输入消息内容' }]},
          title:{rules: [{ required: true, message: '请输入消息标题' }]},
        },
        url: {
          add: "/gmessage/gMessage/add",
          edit: "/gmessage/gMessage/edit",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.gamesUser = ''
        this.flag = false
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.gamesUser = record.userId;
        if(record.remark1==1){
          this.flag = true;
        }else{
          this.flag = false;
        }

        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'userId','title','content','delflag','remark','remark1','remark2','remark3','remark4','remark5','remark6','remark7','remark8','remark9','remark10'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化

            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },

      changeRem(e){
        if(e == 1){
          this.flag = true;
        }else{
          this.flag = false;
          this.gamesUser = ''
        }
      },

      getGamesUser(value){
        console.log(value),
        this.form.setFieldsValue({
          userId: value,
        })
      },

      getContent(e){
        if(e != null){
          this.contentFlagNo = false;
          this.contentFlagYes = true;
        }else{
          this.contentFlagNo = true;
          this.contentFlagYes = false;
        }
        getAction('/commonuse/commonUse/getContentByType', { 'type': e }).then((res) => {
          this.contentList = []
          if (res.success) {
            this.contentList = res.result
          } else {
            console.log(res.message)
          }
        })
      },
    }
  }
</script>

<style lang="less" scoped>

</style>